//
//  ContentView.swift
//  studay
//
//  Created by APPLE on 2024/12/16.
//

import SwiftUI

let formatter = DateFormatter()

func initUserData()->[SingleToDo]{
    var output:[SingleToDo] = []
    formatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
    if let storeData = UserDefaults.standard.object(forKey: TODO_KEY) as? Data {
        let data = try! decoder.decode([SingleToDo].self, from: storeData)
        for item in data {
            if(!item.deleted){
                output.append(SingleToDo(title:item.title,duedate:item.duedate,id:output.count,isFavorite: item.isFavorite))
            }
        }
        
    }
    
    return output
}

struct ContentView: View {
    
    @ObservedObject var todo:ToDo = ToDo(toDoList: initUserData())
    
    @State var isPresented:Bool = false
    
    
    @State var editMode:Bool = false
    
    @State var isShowLike:Bool = false
    
    
    var body: some View {
        
        
        ZStack{
            NavigationView{
                ScrollView(.vertical,showsIndicators: true){
                    VStack{
                        ForEach(self.todo.toDoList){ item in
                            if(!item.deleted ){
                                if(item.isFavorite && isShowLike){
                                    SingleCardView(index:item.id,editMode: $editMode)
                                        .environmentObject(self.todo)
                                        .padding(.top)
                                        .padding(.horizontal)
                                        .animation(.spring(),value:UUID())
                                        .transition(.slide)
                                } else if(!isShowLike){
                                    SingleCardView(index:item.id,editMode: $editMode)
                                        .environmentObject(self.todo)
                                        .padding(.top)
                                        .padding(.horizontal)
                                        .animation(.spring(),value:UUID())
                                        .transition(.slide)
                                }
                                 
                            }
                            
                            
                        }
                    }
                }.navigationTitle("提醒事项")
                .navigationBarItems(trailing: HStack(spacing:10){
                    if editMode
                    {
                        DelMutilButton().environmentObject(todo)
                        ShowLikeButton(isShowLike: $isShowLike)
                    }
                    EditModeButton(editMode: $editMode).environmentObject(todo)
                })
            }
            
            Spacer()
            HStack{
                Spacer()
                VStack{
                    Spacer()
                    Button( action:{
                            isPresented.toggle()
                        } ){
                        Image(systemName: "plus.circle.fill")
                            .resizable()
                            .scaledToFit()
                            .frame(width: 80)
                            .foregroundColor(.blue)
                            .padding(.trailing)
                    }.sheet(isPresented: $isPresented){
                        EditView()
                            .environmentObject(todo)
                    }
                }
            }
        }
   
    }
}



struct EditModeButton:View {
    
    @Binding var editMode:Bool
    @EnvironmentObject var todo:ToDo
    var body: some View {
        Button(action: {
            editMode.toggle()
            if(!editMode){
                todo.clearMultipe()
            }
        }){
            Image(systemName: "gear")
        }
    }
}

struct DelMutilButton :View {
    @EnvironmentObject var todo:ToDo;
    var body:some View {
        Button(action:{
            todo.delMultiple()
        }){
            Image(systemName: "trash")
        }
    }
}

struct ShowLikeButton:View {
    @Binding var isShowLike:Bool
    var body:some View{
        Button(action: {
            isShowLike.toggle()
        }){
            Image(systemName: isShowLike ? "star.fill" :"star")
            
                .foregroundColor(.yellow)
               
                 
                
        }
       
    }
}

struct SingleCardView:View {
    
    @EnvironmentObject var toDo:ToDo
    var index:Int
    @State var isPresented = false
    @Binding var editMode:Bool
    
    var colorIndex:Int {
        get { index % 5 }
    }
    
    var body: some View {
        
        HStack{
            Rectangle()
                .frame(width:6)
                .foregroundColor(Color("Card\(colorIndex)"))
            
            if(editMode){
                Button(action:{
                    self.toDo.del(id: self.index)
                }){
                    Image(systemName: "trash")
                        .imageScale(.large)
                        .padding(.leading)
                        .foregroundColor(.black)
                }
            }
            
           
                
            Button(
                action:{
                    if(!editMode){
                        self.isPresented.toggle()
                    }
                    
                }
            ) {
                Group{
                    
                    VStack(alignment:.leading ,spacing: 6){
                        Text(self.toDo.toDoList[self.index].title)
                            .font(.headline)
                            .fontWeight(.heavy)
                            .foregroundColor(.black)
                        Text( formatter.string(from:self.toDo.toDoList[self.index].duedate))
                            .font(.subheadline)
                            .foregroundColor(.gray)
                    }
                    .padding(.leading)
                    Spacer()
                }
            }.sheet(isPresented: $isPresented ){
                
                
                
                EditView(
                    title:self.toDo.toDoList[self.index].title,
                    duedate:self.toDo.toDoList[self.index].duedate,
                    isFavorite:self.toDo.toDoList[self.index].isFavorite,
                    id:self.index
                )
                    .environmentObject(toDo)
                
                
            }
            
            if editMode {
                
                Image(systemName: toDo.toDoList[index].isFavorite ? "star.fill" :"star")
                    .imageScale(.large)
                    .padding(.trailing)
                    .foregroundColor(.yellow)
                    .onTapGesture {
                        toDo.favorite(id:index)
                    }
                
                Image(systemName: toDo.toDoList[index].selected ? "checkmark.circle.fill" : "circle")
                    .imageScale(.large)
                    .padding(.trailing)
                    .onTapGesture {
                        toDo.multiple(id:index)
                    }
                
                
                
                
            } else{
                Image(systemName: self.toDo.toDoList[index].isCheck ? "checkmark.square.fill" : "square")
                    .imageScale(.large)
                    .padding(.trailing)
                    .onTapGesture {
                        toDo.check(id:index)
                    }
            }
            
            
              
        }.frame(height:80)
        .background(Color.white)
        .cornerRadius(10)
        .shadow(radius:10,x:0,y:10)
        
     
    }
}

#Preview {
    ContentView()
}
